package handler

import (
	"car-trade-mgr/cache"
	"car-trade-mgr/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	CarName := c.PostForm("car_name")
	Number := c.PostForm("number")
	Kind := c.PostForm("kind")
	Price, _ := strconv.Atoi(c.PostForm("price"))
	if CarName == "" || Number == "" || Kind == "" || Price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "参数不能为空",
		})
		return
	}
	data := models.Car{
		CarName: CarName,
		Number:  Number,
		Kind:    Kind,
		Price:   Price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "添加成功",
	})
	return
}

func CarOrderAdd(c *gin.Context) {
	Cid, _ := strconv.Atoi(c.PostForm("cid"))
	OrderNumber := c.PostForm("order_number")
	Price, _ := strconv.Atoi(c.PostForm("price"))
	if OrderNumber == "" || Cid == 0 || Price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "参数不能为空",
		})
		return
	}
	data := models.Order{
		Cid:         Cid,
		OrderNumber: OrderNumber,
		Price:       Price,
	}
	var car models.Car
	err := models.DB.Where("id = ?", Cid).First(&car).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败，车辆信息查询不存在",
		})
		return
	}
	if car.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败，车辆不存在",
		})
		return
	}
	err = models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "添加成功",
	})
	return
}

func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	if size > 50 {
		size = 50
	}
	offset := (page - 1) * size
	/*
		   SELECT
		  orders.id AS OrderId,
		  orders.order_number AS OrderNumber,
		  cars.car_name AS CarName,
		  cars.number AS Number,
		  orders.price AS Price,
		  orders.created_at AS CreatedAt
		FROM
		  orders
		  LEFT JOIN cars ON cars.id = orders.cid
	*/
	var order []models.CarOrder
	redis_key := "orders_list_" + strconv.Itoa(page)
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if redisData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderId",
				"orders.order_number AS OrderNumber",
				"cars.car_name AS CarName",
				"cars.number AS Number",
				"orders.price AS Price",
				"orders.created_at AS CreatedAt",
			).Joins("LEFT JOIN cars ON cars.id = orders.cid").
			Offset(offset).Limit(size).Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &order)
	}
	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表获取成功",
		"data":  order,
		"count": count,
	})
	return
}
func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var order models.CarOrder
	redis_key := "orders_detail_" + id
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if redisData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderId",
				"orders.order_number AS OrderNumber",
				"cars.car_name AS CarName",
				"cars.number AS Number",
				"orders.price AS Price",
				"orders.created_at AS CreatedAt",
			).Joins("LEFT JOIN cars ON cars.id = orders.cid").
			Debug().
			Where("orders.id = ?", id).Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &order)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表获取成功",
		"data": order,
	})
	return
}
